function Tab (config) {
    this._root = config.root;
    this._currentClass = config.currentClass;
    var trigger = config.trigger || "click";
    this._handler = config.handler;
    var autoPlay = config.autoPlay;
    var playTime = config.playTime || 3000;
    this._tabMenus = this._root.find(".J-tab-menu");
    this._tabContents = this._root.find(".J-tab-content");
    this.currentIndex = 0;
    var This = this;
    if( autoPlay ){
        setInterval(function() {This._autoHandler()},playTime);
    }
    for(var i = 0 ; i < this._tabMenus.length; i++){
        var currentTab = this._tabMenus.eq(i);
        (function(i) {
             currentTab.bind(trigger,function() {
                 This.showItem(i);
                 this.currentIndex = i;
             });
        })(i);
    }
}
Tab.prototype = {
    showItem:function(n) {
        for(var i = 0 ; i < this._tabContents.length; i++){
            this._tabContents.eq(i).css("display","none");
        }
        this._tabContents.eq(n).css("display","block");
        if(this._currentClass){
            var currentMenu = this._root.find("." + this._currentClass).eq(0);
            if(currentMenu){
                $(currentMenu).removeClass(this._currentClass);
            }
            $(this._tabMenus.eq(n)).addClass(this._currentClass);
        }
        if( this._handler ){
            this._handler(n);
        }
    },
    _autoHandler:function() {
        this.currentIndex++;
        if(this.currentIndex >= this._tabMenus.length){
            this.currentIndex = 0;
        }
        this.showItem(this.currentIndex);
    }
}
$(document).ready(function() {
        var tabs = $(".tab");
        new Tab({root:tabs.eq(0),currentClass:"tab-currentMenu",trigger:"click"});
        new Tab({root:tabs.eq(1),currentClass:"tab-currentMenu",autoPlay:true,playTime:1000});
        new Tab({root:tabs.eq(2),currentClass:"tab-currentMenu2",trigger:"mouseover",handler:function(index) {alert("您激活的是第" + (index + 1) + "个标签")}});
});
